package com.tunnel.client.udp;

import com.tunnel.client.config.IpAndPort;
import com.tunnel.client.task.UdpTaskManager;
import com.tunnel.client.tunnel.TunnelChannelHandlerContextManager;

import io.netty.channel.ChannelHandlerContext;

public class UdpDataSenderManager {

    public volatile static UdpDataQueue QUEUE = new UdpDataQueue();
    
    public static void startQueue() {
    	QUEUE.startQueue();
	}
    
	public static UdpDataQueue getQueue() {
		return QUEUE;
	}
	
	public static void commitData(IpAndPort iap,byte[] senderAddressBytes,byte[] senderPortBytes,byte[] dataBytes){
		UdpData data = new UdpData(iap,senderAddressBytes,senderPortBytes,dataBytes);
		boolean addSuccess = QUEUE.addDataToQueue(data);
		while(!addSuccess){
			try {
				Thread.sleep(100);
			} catch (Exception e) {}
			addSuccess = QUEUE.addDataToQueue(data);
		}
    }
    
	public static void doSendData(UdpData udpData){
		ChannelHandlerContext ctx = TunnelChannelHandlerContextManager.getChannelHandlerContext();
		UdpTaskManager.newTask(udpData, ctx);
    }
}
